package org.dizena.quartz.adapter.job;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dizena.quartz.bean.dto.QuartzJobResult;
import org.dizena.quartz.dao.QuartzJobResultMapper;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class SingleJob implements Job {

    @Resource
    private QuartzJobResultMapper resultMapper;

    @Override
    public void execute(JobExecutionContext context) {
        JobDetail jobDetail = context.getJobDetail();
        Trigger trigger = context.getTrigger();

        Map jobData = jobDetail.getJobDataMap().getWrappedMap();
        Map triggerData = trigger.getJobDataMap().getWrappedMap();
        log.info("test job data:{}", jobData);
        log.info("test trigger data:{}", triggerData);
        QuartzJobResult cmd = new QuartzJobResult();
        cmd.setJobGroup(jobDetail.getKey().getGroup());
        cmd.setJobName(jobDetail.getKey().getName());
        cmd.setJobParam(String.valueOf(jobData.get("url")));
        cmd.setJobMethod(String.valueOf(jobData.get("method")));
        cmd.setStartTime(System.currentTimeMillis());
        cmd.setRunStatus(0);
        cmd.setRunProcess(0);
        cmd.setActionData(triggerData.toString());
        resultMapper.insert(cmd);
    }
}
